<?php

/**
 *   OPENSHOP_taobao 物流公司关联
 * 
 *   @link        http://baison.com.cn
 *   @copyright   Baison, Inc.
 *   @package     OpenShop
 *   @version     $Id: api_tb_goods.php,v 1.0 2009/11/19 07:35:58 add $
 *   @author      winner <jsycywm@hotmail.com>
 */

define('IN_OS', true);

require_once(dirname(__FILE__).'/includes/init.php');

require_once(ROOT_PATH . '/admin/includes/lib_goods.php');
require_once(ROOT_PATH . '/includes/modules/taobao.api/cls_api.php');
$exc = new exchange($os->table('tb_itme'),$db,'id','num_iid');
	
/* act操作项的初始化 */
if (empty($_REQUEST['act'])){
    $_REQUEST['act'] = 'list';
}else{
    $_REQUEST['act'] = trim($_REQUEST['act']);
}

if($_REQUEST['act']=='list')
{	 
//	admin_priv(); 	//检查权限
   if( $shipping_id = $_REQUEST['status'] ){
		   Update_table_key('shipping','defalut_delivery','0','1=1','shipping_id'); 
		   Update_table_key('shipping','defalut_delivery','1',"shipping_id='".$shipping_id."'",'shipping_id'); 
	   } 
	$smarty->assign('ur_here',      '物流公司关联');    
	$logistics_list = get_tb_logisticslist();   
	$smarty->assign('seleced_os_shipping',  $db->getOne("select shipping_id from open_shipping where defalut_delivery=1")) ;
	$smarty->assign('os_shipping',  $db->getAll("select shipping_id,shipping_name from open_shipping"));
	$smarty->assign('logistics_list',   $logistics_list['goods']);
    $smarty->assign('filter',       $logistics_list['filter']);
    $smarty->assign('record_count', $logistics_list['record_count']);
    $smarty->assign('page_count',   $logistics_list['page_count']);		 
	$smarty->assign('full_page',    1); 
    assign_query_info();    
	$smarty->display('api_tb_logistics_list.tpl');	 
} elseif ($_REQUEST['act'] == 'logistics_down'){
	InsertIntoTB_shipping();
	echo ('下载成功! <a href="#" onclick="history.go(-1)">返回</a>');
} elseif ($_REQUEST['act'] == 'query'){
    $logistics_list = get_tb_logisticslist(); 
    $smarty->assign('logistics_list',   $logistics_list['goods']);
    $smarty->assign('filter',       $logistics_list['filter']);
    $smarty->assign('record_count', $logistics_list['record_count']);
    $smarty->assign('page_count',   $logistics_list['page_count']); 
    make_json_result($smarty->fetch('api_tb_logistics_list.tpl'), '',
     array('filter' => $logistics_list['filter'], 'page_count' => $logistics_list['page_count']));
}elseif($_REQUEST['act']=='relevancy'){    
	$smarty->assign('action_link',  array('href' => 'api_tb_logistics.php?act=list', 'text' => '淘宝物流关联管理'));  
	$num_iid = $_GET['id']   ;     
	$tb_company_row = $db->getRow("select company_code,company_name from open_tb_shipping where id='".$num_iid."'");   
	$smarty->assign('cat_logistics_list',get_tb_cat_logistics_list( $num_iid ));
	$smarty->assign('num_iid',$num_iid);  
	$smarty->assign('vname','['.$tb_company_row['company_code'].']'.$tb_company_row['company_name']);  
    $smarty->assign('ur_here',      '淘宝物流('.'['.$tb_company_row['company_code'].']'.$tb_company_row['company_name'].')关联设置' ); 
	assign_query_info();
	$smarty->display('api_tb_logistics_relevancy.tpl');	
}
/*------------------------------------------------------ */
//--  
/*------------------------------------------------------ */ 
elseif($_REQUEST['act']=='search_cat'){   //查找关联的 
    include_once(ROOT_PATH . 'includes/cls_json.php'); 
    $json   = new JSON;
    $filter = $json->decode($_GET['JSON']);
    $filter->keyword = json_str_iconv($filter->keyword); 
    $sql = "SELECT shipping_code AS id, CONCAT('[',shipping_code,']', shipping_name) as name FROM " . $os->table('shipping') .
        " WHERE shipping_code LIKE '%" . mysql_like_quote($filter->keyword) . "%' or shipping_name LIKE '%" . mysql_like_quote($filter->keyword) . "%' LIMIT 50";
 
   $arr = $db->getAll($sql);
    if (empty($arr)){
        $arr = array(0 => array(
            'id'   => 0,
            'name' => '没有找到相应记录，请重新搜索'
        ));
    }

    make_json_result($arr);	
    
}elseif ($_REQUEST['act']=='add_cat_item'){ 
    include_once(ROOT_PATH . 'includes/cls_json.php');    
    $json = new JSON; 
    $shiping  =$json->decode(  $_GET['add_ids'] );
    $shiping_code = $shiping[0] ; 
    $arguments  = $json->decode($_GET['JSON']);
    $num_iid   = $arguments[0]; 
	$sql = "update " . $os->table('tb_shipping') . " set field1='".$shiping_code."'where id=". $num_iid; 
	$db->query($sql);  
    $arr = get_tb_cat_logistics_list($num_iid );  
    $opt = array(); 
    foreach ($arr AS $val)
    {
        $opt[] = array('value'      => $val['shipping_code'],
                        'text'      => '['.$val['shipping_code'].']'.$val['shipping_name'],
                        'data'      => '');
    } 
    clear_cache_files(); 
    make_json_result($opt);
    
}elseif($_REQUEST['act']=='drop_cat_item'){  
    include_once(ROOT_PATH . 'includes/cls_json.php');
    $json = new JSON; 
	$shiping =$json->decode(  $_GET['add_ids'] );
	$shiping_code =$goods_sn[0] ; 
	$arguments  = $json->decode($_GET['JSON']);
	$num_iid   = $arguments[0]; 
	$sql = "update " . $os->table('tb_shipping') . " set field1='' where id=". $num_iid;  
    $db->query($sql); 
    $arr = get_tb_cat_logistics_list($num_iid);
    $opt = array(); 
    foreach ($arr AS $val){
        $opt[] = array('value'      => $val['cat_id'],
                        'text'      => $val['cat_name'],
                        'data'      => '');
    }
    
    clear_cache_files();
    make_json_result($opt);	
}

function get_tb_logisticslist(){
	global $_CFG;
    $result = get_filter();
    if ($result === false)
    {
        /* 分页大小 */
        $filter = array();

        $filter['keyword']  = empty($_REQUEST['keyword']) ? '' : trim($_REQUEST['keyword']);

        if ($_REQUEST['is_ajax'] == 1)
        {
            $filter['keyword'] = json_str_iconv($filter['keyword']);
        }        
        
        if ($filter['keyword'])
        {

			$where .= " AND (tb_s.company_code LIKE '%" . mysql_like_quote($filter['keyword']) . "%' 
						OR tb_s.company_name LIKE '%" . mysql_like_quote($filter['keyword']) . "%' 
						)";
        }     
                
        /* 记录总数以及页数 */
        $sql = "SELECT COUNT(*) FROM ".$GLOBALS['os']->table('tb_shipping')." as tb_s  
        	    left join open_shipping as s on tb_s.field1=s.shipping_code
        		where 1  ".$where;  //仅显示当应用的商品 
        $filter['record_count'] = $GLOBALS['db']->getOne($sql); 
        $filter = page_and_size($filter); 
         /* 查询记录 */ 
		$sql = "select *
				from open_tb_shipping as tb_s 
				left join open_shipping as s on tb_s.field1=s.shipping_code
        		where 1 ".$where." ORDER BY  tb_s.id DESC";  
        /* 查询记录 */ 
        set_filter($filter, $sql);
    }
    else{ 
        $sql    = $result['sql'];
        $filter = $result['filter'];
    } 
    $res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);
    
    $arr = array();
    while ($rows = $GLOBALS['db']->fetchRow($res))  {    
        $arr[] = $rows;
    }	 
    return array('goods' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}
	 

function get_tb_cat_logistics_list($num_iid=0){ 
	static $rows = NULL;  
    $sql ="select * from open_tb_shipping as tb_s
 			left join open_shipping as s on tb_s.field1 = s.shipping_code "; 
	if($num_iid>0){
		$sql .= " WHERE tb_s.id = '$num_iid'";
	}
	$sql .= " ORDER BY tb_s.id DESC";   
	if ($rows === NULL){
		$rows = $GLOBALS['db']->getAll($sql);
	}
	return $rows;		
}
?>